package com.example.yls.demoa;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyService extends Service {

    private boolean isLove = true;
    private boolean isLive = true;

    private MyBinder myBinder;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("MyService", "thread id =" + Thread.currentThread().getId());
        new Thread(new Runnable() {

  public void run(){
         while(isLive){
             Intent intent=new Intent();
             intent.setAction("TIME_CHANGE");
             intent.putExtra("time",getTimeStr());
             sendBroadcast(intent);

             try{
                 Thread.sleep(2000);
             }
             catch(InterruptedException e){
                 e.printStackTrace();
             }
         }
      }
  }).start();
}
      private String getTimeStr() {
         Date date =new Date();
          SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
          return format.format(date);
      }
      public void onDestroy(){
         isLive=false;
         super.onDestroy();
      }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("MyService", "onBind");
        return null;
    }

    class MyBinder extends Binder {
        public int add(int a, int b) {
            return a + b;
        }

        public String toUpperCase(String s) {
            Log.e("MyService", "onBind");

            return null;
        }
    }
}
